package com.bangdream.michelia.view.activity.currency.personal;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.bangdream.michelia.R;
import com.bangdream.michelia.application.AppCurrentUser;
import com.bangdream.michelia.contract.UserContract;
import com.bangdream.michelia.entity.BaseEntity;
import com.bangdream.michelia.entity.UserMsgBean;
import com.bangdream.michelia.presenter.UserPresenter;
import com.bangdream.michelia.tool.retrofithttp.RetroFactory;
import com.bangdream.michelia.utils.SPUtils;
import com.bangdream.michelia.utils.ToastUtil;
import com.bangdream.michelia.view.activity.base.SwipeBackActivity;
import com.google.gson.Gson;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @date on 2018/8/11 15:09
 * @email 909732184@qq.com
 * 个人中心
 */
public class PersonalCenterActivity extends SwipeBackActivity<UserContract.IUserView, UserPresenter<UserContract.IUserView>> implements View.OnClickListener, UserContract.IUserView, UserContract.IUserEditView {
    private XRefreshView refreshView;
    private RelativeLayout btnName, btnRePassWord, btnPosition, btnCity, btnSex;
    TextView tvName, tvSex, tvPhoneNumber, tvPosition, tvCity, tvPassword;
    UserMsgBean userMsgBean;
    Button btComplete;

    @Override
    public int getLayout() {
        return R.layout.activity_personal_center;
    }

    @Override
    public void findId() {
        refreshView = findViewById(R.id.refreshView);
        refreshView.setPullRefreshEnable(false);
        refreshView.setPullLoadEnable(false);
        btnName = findViewById(R.id.btnName);
        btnRePassWord = findViewById(R.id.btnRePassWord);
        btnPosition = findViewById(R.id.btnPosition);
        btnCity = findViewById(R.id.btnCity);
        btnSex = findViewById(R.id.btnSex);
        tvName = findViewById(R.id.tvName);
        tvSex = findViewById(R.id.tvSex);
        tvPassword = findViewById(R.id.tvPassword);
        tvPhoneNumber = findViewById(R.id.tvPhoneNumber);
        tvPosition = findViewById(R.id.tvPosition);
        tvCity = findViewById(R.id.tvCity);
        btComplete = findViewById(R.id.bt_complete);

        initData();
    }

    private void initData() {
//        String id = AppCurrentUser.getInstance().getUserInfo().getId();
//        mPresenter.getUserInfo(id,AppCurrentUser.getInstance().getToken());
        Serializable serializable = getIntent().getSerializableExtra("userMsgBean");

        if (serializable != null) {
            userMsgBean = (UserMsgBean) serializable;
            tvName.setText(userMsgBean.getNickname());
            tvSex.setText(userMsgBean.getSex().equals("F") ? "女" : "男");
            tvPhoneNumber.setText(userMsgBean.getTelPhone());
//            tvPosition.setText(userMsgBean.setUsername(););
//            tvCity.setText(userMsgBean.get);
        }
    }

    @Override
    public void setListener() {
        btnName.setOnClickListener(this);
        btnRePassWord.setOnClickListener(this);
        btnPosition.setOnClickListener(this);
        btnCity.setOnClickListener(this);
        btnSex.setOnClickListener(this);
        btComplete.setOnClickListener(this);
    }

    @Override
    public void logic() {
        initTitleBar(" ", "资料设置", "", this);
    }

    @Override
    public UserPresenter<UserContract.IUserView> createPresenter() {
        return new UserPresenter<>();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        ModifyUserData.ModifyType type = null;

        switch (view.getId()) {
            case R.id.btnName:
//                type=ModifyUserData.ModifyType.NameType;
                showEditDialog("完善昵称", 0);
                break;
            case R.id.btnRePassWord:
//                type=ModifyUserData.ModifyType.PwdType;
                showEditDialog("修改密码", 1);
                break;
            case R.id.btnPosition:
//                type=ModifyUserData.ModifyType.PositionType;
                showEditDialog("修改职位", 2);

                break;
            case R.id.btnCity:
                showEditDialog("修改城市", 3);
                break;
            case R.id.btnSex:
                sexDialog();
                break;
            case R.id.bt_complete:
                save();
                break;
            default:

        }
//        Intent intent = new Intent(this,ModifyUserData.class);
//        intent.putExtra("type",type);
//        startActivity(intent);
    }


    private void save() {
        Map<String, String> map = new HashMap<>();
        map.put("nickname", tvName.getText().toString());
        String sex = tvSex.getText().toString().equals("女") ? "F" : "M";
        map.put("sex", sex);

        mPresenter.editUser(map,pd);

    }

    @Override
    public void setRegisterCode(boolean isSuccess, String stDescribe) {

    }

    @Override
    public void actionRegister(boolean isSuccess, String stDescribe) {

    }

    @Override
    public void actionLogin(boolean isSuccess, String stDescribe) {

    }

    @Override
    public void actionFindPassword(boolean isSuccess, String stDescribe) {

    }

    @Override
    public void setUserMsg(boolean isSuccess, UserMsgBean userMsgBean) {

    }

    private void showEditDialog(String title, final int positon) {

        final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(this);
        builder.setTitle(title)
                .setInputType(InputType.TYPE_CLASS_TEXT)
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        String s = builder.getEditText().getText().toString();
                        if (s.isEmpty()) {
                            ToastUtil.showShortToast(PersonalCenterActivity.this, "请填入信息");
                        } else {
                            dialog.dismiss();
                            switch (positon) {
                                case 0:
                                    tvName.setText(s);
                                    break;
                                case 1:
//                                    tvPassword.setText(s);
                                    editpwd(s);
                                    break;
                                case 2:
                                    tvPosition.setText(s);
                                    break;
                                case 3:
                                    tvCity.setText(s);
                                    break;
                            }

                        }

                    }
                }).show();

    }

    private void sexDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View v = LayoutInflater.from(this).inflate(R.layout.choose_dialog, null);
        TextView man = v.findViewById(R.id.man);
        TextView woman = v.findViewById(R.id.woman);
        final AlertDialog dialog = builder.create();
        dialog.show();
        dialog.getWindow().setContentView(v);
        man.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                tvSex.setText("男");
            }
        });
        woman.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                tvSex.setText("女");
            }
        });
    }

    @Override
    public void isSuccessed(boolean isSuccess) {
        if(isSuccess){
            ToastUtil.showShortToast(PersonalCenterActivity.this,"保存成功");
            finish();
        }
        else  ToastUtil.showShortToast(PersonalCenterActivity.this,"保存失败");

    }


    private void editpwd(String pwd) {
        Map map = new HashMap();
        map.put("password", pwd);
        map.put("userId", AppCurrentUser.getInstance().getUserInfo().getId());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), new Gson().toJson(map));
        String stTokent = AppCurrentUser.getInstance().getToken();

        disposable= RetroFactory.getInstance().editPwd(stTokent,requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseEntity>() {

                    @Override
                    public void accept(BaseEntity baseEntity) throws Exception {
                        if(baseEntity.isOk()){
                            ToastUtil.showShortToast(PersonalCenterActivity.this, "密码修改成功");
                            SPUtils.put(PersonalCenterActivity.this,"pwd",pwd);
                        }else{
                            ToastUtil.showShortToast(PersonalCenterActivity.this, "密码修改失败");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        ToastUtil.showShortToast(PersonalCenterActivity.this, "密码修改失败");
                    }
                });
    }
}
